package GUI;

import UserData.User_Management;

import com.toedter.calendar.JDateChooser;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.JComboBox;

public class MyPage extends JPanel
{
	private JPanel panelMypage;
	private JTextField txtfMPSearch;
	private JTable table;
	private JScrollPane scrollPane;
	private JList list;
	private Object[][] data;
	private String MPSearchStr;
	private String MPSearchlowstr;
	private String MPSearchupstr;
	private String samestr;
	private Calendar cal;
	private int day;
	private int mon;
	private int lastday;
	private String[] whatday;
	private boolean MPflag;
	private int[][] temp;
	private int rowIndexStart;
	private int rowIndexEnd;
	private int colIndex;
	private JComboBox comboBox;
	private  DefaultTableModel dm;


	public MyPage()
	{
		setBounds(0, 0, 745, 489);
		setLayout(null);

		this.panelMypage = new JPanel();

		this.panelMypage.setBounds(0, 0, 745, 489);
		add(this.panelMypage);

		this.cal = Calendar.getInstance();
		this.cal.get(5);
		this.mon = (this.cal.get(2) + 1);
		this.lastday = this.cal.getActualMaximum(Calendar.DATE);
		this.whatday = new String[this.lastday];
		this.temp = new int[this.lastday][48];

		for (int i = 0; i < this.lastday; i++) {
			this.whatday[i] = (this.mon + "/" + (i + 1));
		}
		panelMypage.setLayout(null);

		this.txtfMPSearch = new JTextField();
		txtfMPSearch.setBounds(320, 24, 180, 40);
		this.txtfMPSearch.setBorder(null);
		this.panelMypage.add(this.txtfMPSearch);
		this.txtfMPSearch.setColumns(10);

		JButton btnMPSearch = new JButton("Search") {
			public void paintComponent(Graphics g) {
				Image image = new ImageIcon("iconsearch.png").getImage();
				g.drawImage(image, 0, 0, this);
			}
		};
		btnMPSearch.setBounds(509, 23, 80, 40);
		btnMPSearch.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0) {
				MyPage.this.SerchActionPerformed(arg0);

			}
		});
		btnMPSearch.setBorder(null);
		this.panelMypage.add(btnMPSearch);

		//this.table1=new schTable();

		this.data = new Object[26][this.lastday];
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 31; j++) {
				data[i][j] = User_Management.Now_User.Sche_List[i][j].getScheduleName();

			}

		}



		dm = new DefaultTableModel(this.data, this.whatday);

		this.scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 80, 679, 370);
		this.panelMypage.add(this.scrollPane);

		this.table = new JTable(dm)
		{

			public boolean isCellEditable(int row, int column)
			{
				
				return false;
			}
		};
		this.table.setRowHeight(50);
		this.table.setAutoResizeMode(0);
		this.table.setColumnSelectionAllowed(true);
		this.table.setRowSelectionAllowed(true);

		DefaultTableCellRenderer tScheduleCellRenderer = new DefaultTableCellRenderer();
		tScheduleCellRenderer.setHorizontalAlignment(0);
		TableColumnModel tcmSchedule = this.table.getColumnModel();
		for (int i = 0; i < tcmSchedule.getColumnCount(); i++) {
			tcmSchedule.getColumn(i).setCellRenderer(tScheduleCellRenderer);
		}
		this.scrollPane.setViewportView(this.table);

		this.list = new JList(new AbstractListModel() {
			String[] values = { "09:00~09:30", "09:30~10:00", "10:00~10:30", "10:30~11:00", "11:00~11:30", "11:30~12:00", "12:00~12:30", "12:30~13:00", "13:00~13:30", "13:30~14:00", "14:00~14:30", "14:30~15:00", "15:00~15:30", "15:30~16:00", "16:00~16:30", "16:30~17:00", "17:00~17:30", "17:30~18:00", "18:00~18:30", "18:30~19:00", "19:00~19:30", "19:30~20:00", "20:00~20:30", "20:30~21:00", "21:00~21:30", "21:30~22:00" };

			public int getSize() { return this.values.length; }

			public Object getElementAt(int index) {
				return this.values[index];
			}
		});
		this.list.setFixedCellWidth(100);
		this.list.setFixedCellHeight(50);
		this.list.setCellRenderer(new RowHeaderRenderer(this.table));
		this.scrollPane.setRowHeaderView(this.list);

		JButton btnNewButton = new JButton("Update");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ScheduleRepaint();
			}
		});


		btnNewButton.setBounds(12, 23, 80, 40);
		panelMypage.add(btnNewButton);

		this.table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					double row = MyPage.this.table.rowAtPoint(e.getPoint());

					int rowIndex = MyPage.this.table.rowAtPoint(e.getPoint());
					int colIndex = MyPage.this.table.columnAtPoint(e.getPoint());
					//System.out.println("rowIndex:" + rowIndex);
					//System.out.println("colIndex:" + colIndex);

					rowIndexStart = MyPage.this.table.rowAtPoint(e.getPoint());
					//System.out.println("row:" + row);

					if (row % 2.0D == 0.0D) {
						row = row / 2.0D + 9.0D;
					}
					else {
						row = (int)row / 2 + 9.5D;
					}
					int col = MyPage.this.table.columnAtPoint(e.getPoint());

					col++;

					String source = null;
					int intRow = (int)row;
					if (row - intRow == 0.0D)
						source = "2014-12-" + col + "-" + intRow + "-" + "00";
					else if (row - intRow == 0.5D) {
						source = "2014-12-" + col + "-" + intRow + "-" + "30";
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
					Date date = null;
					try {
						date = sdf.parse(source);
					}
					catch (ParseException e1) {
						e1.printStackTrace();
					}
					

					Registration register = new Registration();
					register.getDateChooser().setDate(date);
					register.getCmbStartHour().setSelectedIndex(intRow-9);

					register.setRowIndexStart(rowIndex);
					register.setColIndex(colIndex);

					if(row-intRow==0.5){
						register.getCmbStartMin().setSelectedIndex(1);
						register.getCmbFinishHour().setSelectedIndex(intRow-8);
						register.getCmbFinishMin().setSelectedIndex(0);
					}else if(row-intRow==0){
						register.getCmbStartMin().setSelectedIndex(0);
						register.getCmbFinishHour().setSelectedIndex(intRow-9);
						register.getCmbFinishMin().setSelectedIndex(1);
					}
					register.setVisible(true);
				}
			}
		});

	}
//Draw a updated table agter schedule registration
	public void ScheduleRepaint(){
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 31; j++) {
				data[i][j]=User_Management.Now_User.Sche_List[i][j].getScheduleName(); 
				dm = new DefaultTableModel(this.data, this.whatday);
				table.setModel(dm);
				DefaultTableCellRenderer tScheduleCellRenderer = new DefaultTableCellRenderer();
				tScheduleCellRenderer.setHorizontalAlignment(0);
				TableColumnModel tcmSchedule = this.table.getColumnModel();
				for (int a = 0; a < tcmSchedule.getColumnCount(); a++) {
					tcmSchedule.getColumn(a).setCellRenderer(tScheduleCellRenderer);
				}
			}      
		}
	}




//Searching date of registered schedule
	public void SerchActionPerformed(ActionEvent e)
	{
		this.MPSearchStr = this.txtfMPSearch.getText();
		//Convert the MPSearchStr text to lowercase
		this.MPSearchlowstr = this.MPSearchStr.toLowerCase();
		//Convert the MPSearchStr text to uppercase
		this.MPSearchupstr = this.MPSearchStr.toUpperCase();
		this.samestr = "";
		this.MPflag = false;
		
		for (int j = 0; j < lastday; j++) {
			int compare =0;
			for (int i = 0; i < 26; i++) {
				if ((this.MPSearchlowstr.equals(this.data[i][j])) || (this.MPSearchupstr.equals(this.data[i][j]))) {
					this.MPflag = true;
					compare++;
					if(compare==1)
						this.samestr = ("\n"+ this.samestr+"Date : " + this.mon + "/" + (j + 1));

				}
			}
			
		}
		if (!this.MPflag)
			JOptionPane.showMessageDialog(this.panelMypage, "NO FOUND");
		else
			JOptionPane.showMessageDialog(this.panelMypage,"Date of Search Subject"+ this.samestr);
	}



	class RowHeaderRenderer extends JLabel
	implements ListCellRenderer
	{
		RowHeaderRenderer(JTable table)
		{
			JTableHeader header = table.getTableHeader();
			setOpaque(true);
			setBorder(UIManager.getBorder("TableHeader.cellBorder"));
			setHorizontalAlignment(0);
			setForeground(header.getForeground());
			setBackground(header.getBackground());
			setFont(header.getFont());
		}

		public Component getListCellRendererComponent(JList arg0, Object arg1, int arg2, boolean arg3, boolean arg4)
		{
			setText(arg1 == null ? "" : arg1.toString());
			return this;
		}
	}

}